const mongoose = require('mongoose')
const logger = require('../utils/logger') // 使用别名导入

const commomProdUrl =
  'mongodb://admin:20190808@8.140.244.5:27017/dyxw?authSource=admin'
// const commomDevUrl = 'mongodb://127.0.0.1:27017/dyxw'
const commomDevUrl =
  'mongodb://admin:20190808@8.140.244.5:27017/dyxw?authSource=admin'

const connectUrl =
  process.env.NODE_ENV === 'development' ? commomDevUrl : commomProdUrl

const connectDB = async () => {
  try {
    await mongoose.connect(connectUrl)
    logger.info('******** 数据库连接成功！********')
  } catch (error) {
    logger.error('******** 数据库连接失败！********', error)
    process.exit(1)
  }
}

module.exports = connectDB
